package FiveOne;

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Description：
 * Author: zhangc
 * Date：2017/9/18 18:57
 */
public class Main {
    private static volatile AtomicInteger total = new AtomicInteger(0);
    private static Queue<String> queue = new ConcurrentLinkedQueue<>();
    private static ExecutorService threadPool = Executors.newFixedThreadPool(3);

    public static void main(String[] args) throws InterruptedException {

        CountDownLatch latch = new CountDownLatch(3);
        String[] str = {"11u51fcd", "xcfcxv", "u51dfsjfd", "51nju51", "51udhdj", "uu51sdfjhsfdhj"};
        for (String s : str)
            queue.add(s);
        for (int i = 0; i < 3; i++)
            threadPool.execute(new Handler(latch));
        latch.await();      //屏障在这里
        threadPool.shutdown();
        System.out.println(total.getAndIncrement());

    }

    static class Handler implements Runnable {
        private String str;
        private CountDownLatch latch;

        public Handler(CountDownLatch latch) {
            this.latch = latch;
        }

        @Override
        public void run() {
            while (!queue.isEmpty()) {
                str = queue.remove();
                if (str.indexOf("u51") >= 0)
                    total.incrementAndGet();
            }
            latch.countDown();//计数器减一
        }
    }
}
